package com.tjp.eight.ui.fragment.rank;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.tabFind.FindResultModel;
import com.tjp.eight.utils.StringUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/17.
 */

public class RankAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<FindResultModel.FindItemModel> models;
    private RankItemOnClick itemOnClick;
    private boolean noRank = true;

    public RankAdapter(Context context) {
        this.context = context;
        models = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RankViewHolder(LayoutInflater.from(context).inflate(R.layout.item_rank_article, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int pos = position + 1;
        final int p = position;
        ((RankViewHolder) holder).tvRank.setText(pos + "");
        if (!noRank) {
            ((RankViewHolder) holder).tvRank.setVisibility(View.GONE);
        } else {
            ((RankViewHolder) holder).tvRank.setVisibility(View.VISIBLE);
        }
        Glide.with(context).load(models.get(position).cover_path).error(R.drawable.loading).into(((RankViewHolder) holder).iv_rank_pic);
        ((RankViewHolder) holder).tv_rank_title.setText("" + models.get(position).title);
        ((RankViewHolder) holder).tv_rank_type.setText("" + models.get(position).cat_name);
        ((RankViewHolder) holder).tv_rank_views.setText("" + StringUtils.toWan(models.get(position).views_all));
        ((RankViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemOnClick != null) {
                    itemOnClick.itemClick(models.get(p).cid);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return models.size();
    }

    public void addData(ArrayList<FindResultModel.FindItemModel> data) {
        if (data == null || data.isEmpty()) return;
        for (FindResultModel.FindItemModel f : data) {
            models.add(f);
        }
        notifyItemChanged(getItemCount());
        notifyDataSetChanged();
    }

    public void addForSearche(ArrayList<FindResultModel.FindItemModel> data) {
        if (data == null || data.isEmpty()) return;
        models.clear();
        for (FindResultModel.FindItemModel f : data) {
            models.add(f);
        }
        notifyItemChanged(getItemCount());
        notifyDataSetChanged();
    }

    public void setItemOnClick(RankItemOnClick itemOnClick) {
        this.itemOnClick = itemOnClick;
    }

    public void setNoRank(boolean noRank) {
        this.noRank = noRank;
    }

    public interface RankItemOnClick {
        void itemClick(String cid);
    }

    class RankViewHolder extends RecyclerView.ViewHolder {
        TextView tvRank;
        TextView tv_rank_title;
        TextView tv_rank_type;
        TextView tv_rank_views;
        ImageView iv_rank_pic;
        View itemView;

        public RankViewHolder(View view) {
            super(view);
            itemView = view;
            tvRank = (TextView) view.findViewById(R.id.tv_rank);
            tv_rank_title = (TextView) view.findViewById(R.id.tv_rank_title);
            tv_rank_type = (TextView) view.findViewById(R.id.tv_rank_type);
            tv_rank_views = (TextView) view.findViewById(R.id.tv_rank_views);
            iv_rank_pic = (ImageView) view.findViewById(R.id.iv_rank_pic);
        }
    }
}
